View Javadoc

1   
2   /*
3    * SmartCrawler
4    *
5    * $Id: SiteConfiguration.java,v 1.5 2005/08/05 15:55:53 vincool Exp $
6    * Copyright 2005 Davide Pozza
7    *
8    * This program is free software; you can redistribute it
9    * and/or modify it under the terms of the GNU General Public
10   * License as published by the Free Software Foundation;
11   * either version 2 of the License, or (at your option) any
12   * later version.
13   *
14   * This program is distributed in the hope that it will be
15   * useful, but WITHOUT ANY WARRANTY; without even the implied
16   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17   * PURPOSE. See the GNU General Public License for more
18   * details.
19   *
20   * You should have received a copy of the GNU General Public
21   * License along with this program; if not, write to the Free
22   * Software Foundation, Inc., 59 Temple Place, Suite 330,
23   * Boston, MA 02111-1307 USA
24   *
25   */
26  
27  package org.smartcrawler.common;
28  import java.io.File;
29  import java.util.Hashtable;
30  import java.util.TreeMap;
31  import org.apache.commons.configuration.Configuration;
32  import org.apache.commons.configuration.XMLConfiguration;
33  
34  /***
35   *
36   *
37   * @author <a href="mailto:pozzad@alice.it">Davide Pozza</a>
38   * @version <tt>$Revision: 1.5 $</tt>
39   */
40  public class SiteConfiguration {
41  
42      public static final int RETRIEVER_CLASS = 1;
43      public static final int PERSISTER_CLASS = 2;
44      public static final int PREC_FILTERS_LIST = 3;
45      public static final int POST_FILTERS_LIST = 4;
46      public static final int ENGINES_THREADS_NUMBER = 5;
47      public static final int LOGGERS = 6;
48      public static final int PERSISTER_ROOTDIR = 7;
49  
50      public static final int INITIAL_LINK = 10;
51  
52      private Configuration conf;
53  
54      private static Link initialLink;
55      //private File rootDir;
56      //private int downloadEngNum;
57      //private TreeMap filters;
58  
59      public SiteConfiguration() {
60          loadConfig("smartcrawler-config.xml");
61      }
62  
63      /***
64       *
65       * @param paramId
66       * @return
67       */
68      public Object get(int paramId) {
69          String item = "";
70          Object res = null;
71          switch(paramId) {
72              case RETRIEVER_CLASS: {
73                  /*
74                  item = this.conf.getString("retriever.class");
75                  try {
76                      res = Class.forName(item).newInstance();
77                  } catch(Exception e){
78                      res = new HttpRetriever();
79                  }
80                   */
81                  break;
82              }
83              case PERSISTER_CLASS: {
84                  /*
85                  item = this.conf.getString("persister.class");
86                  try {
87                      res = Class.forName(item).newInstance();
88                  } catch(Exception e){
89                      res = new HttpRetriever();
90                  }
91                  res = Class.forName(item).newInstance();
92                   */
93                  break;
94              }
95              case PREC_FILTERS_LIST: {
96                  int n = this.conf.getList("retriever.filters.filter.class").size();
97                  TreeMap map = new TreeMap();
98                  for (int i = 0; i < n; i++) {
99                      String key = this.conf.getString(
100                             "retriever.filters.filter(" + i + ").priority");
101                     String value = this.conf.getString(
102                             "retriever.filters.filter(" + i + ").class");
103                     String prevVal = (String)map.put(key, value);
104                     String newKey = key;
105                     while (prevVal != null) {
106                         newKey = "" + (Integer.parseInt(newKey) + 1);
107                         prevVal = (String)map.put(newKey, prevVal);
108                     }
109                 }
110                 res = map.values();
111                 break;
112             }
113             case POST_FILTERS_LIST: {
114                 int n = this.conf.getList("persister.filters.filter.class").size();
115                 TreeMap map = new TreeMap();
116                 for (int i = 0; i < n; i++) {
117                     String key = this.conf.getString(
118                             "persister.filters.filter(" + i + ").priority");
119                     String value = this.conf.getString(
120                             "persister.filters.filter(" + i + ").class");
121                     String prevVal = (String)map.put(key, value);
122                     String newKey = key;
123                     while (prevVal != null) {
124                         newKey = "" + (Integer.parseInt(newKey) + 1);
125                         prevVal = (String)map.put(newKey, prevVal);
126                     }
127                 }
128                 res = map.values();
129                 break;
130             }
131             case ENGINES_THREADS_NUMBER: {
132                 res = this.conf.getInteger(
133                             "engine.threadsNumber",
134                             new Integer("5"));
135                 break;
136             }
137             case LOGGERS: {
138                 int n = this.conf.getList("loggers.logger").size();
139                 Hashtable loggers = new Hashtable();
140                 for (int i = 0; i < n; i++) {
141                     String active = this.conf.getString(
142                             "loggers.logger(" + i + ")[@active]");
143                         String str = this.conf.getString(
144                             "loggers.logger(" + i + ")[@type]");
145                         loggers.put(str, active);
146                 }
147                 res = loggers;
148                 break;
149             }
150             case PERSISTER_ROOTDIR: {
151                 item = this.conf.getString("persister.rootdir");
152                 if (item == null) {
153                     item = ".";
154                 }
155                 res = new File(item);
156                 break;
157             }
158             default: {
159             }
160         }
161         return res;
162     }
163 
164     /***
165      *
166      * @param configFile
167      */
168     public SiteConfiguration(String configFile) {
169         loadConfig(configFile);
170     }
171 
172     /***
173      *
174      * @param configFile
175      */
176     protected void loadConfig(String configFile) {
177         try {
178             this.conf = new XMLConfiguration(configFile);
179         } catch (Exception e) {
180             e.printStackTrace();
181         }
182     }
183 
184     /***
185      *
186      * @return
187      */
188     public Link getInitialLink() {
189         return initialLink;
190     }
191 
192     /***
193      *
194      * @param initialLink
195      */
196     public void setInitialLink(Link initialLink) {
197         this.initialLink = initialLink;
198     }
199 
200 }